#include "stdafx.h"

#include "LoginDlg.h"
#include "SubscriptionsDlg.h"

#define MZ_IDC_USERNAME		100
#define MZ_IDC_PASSWORD		101
#define MZ_IDC_TOOLBAR		102

CGoogleReader Service(_T("M8-RSS Reader 1.0.1"));
CLoginDlg g_LoginDlg;

CLoginDlg::CLoginDlg()
{
}

CLoginDlg::~CLoginDlg()
{
}

BOOL CLoginDlg::OnInitDialog()
{
	if(!CMzWndEx::OnInitDialog()) return FALSE;

	mUserName.SetPos(MZM_MARGIN_MAX, MZM_MARGIN_MAX, GetWidth()-MZM_MARGIN_MAX*2, 70);
	mUserName.SetID(MZ_IDC_USERNAME);
	mUserName.SetTip2(L"Username:");
	mUserName.SetEditStyle(SINGLELINE_EDIT_STYLE_ZOOM_IN|SINGLELINE_EDIT_STYLE_DEFAULT_BG);
	mUserName.SetSipMode(IM_SIP_MODE_MAIL_LETTER);
	mUserName.SetLeftInvalid(150);
	AddUiWin(&mUserName);  

	mPassword.SetPos(MZM_MARGIN_MAX, MZM_MARGIN_MAX+100, GetWidth()-MZM_MARGIN_MAX*2, 70);
	mPassword.SetID(MZ_IDC_PASSWORD);
	mPassword.SetTip2(L"Password:");
	mPassword.SetEditStyle(SINGLELINE_EDIT_STYLE_PASSWORD|SINGLELINE_EDIT_STYLE_ZOOM_IN|SINGLELINE_EDIT_STYLE_DEFAULT_BG);
	mPassword.SetSipMode(IM_SIP_MODE_GEL_LETTER);
	mPassword.SetLeftInvalid(150);
	AddUiWin(&mPassword);  

    mToolbar.SetPos(0,GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR,GetWidth(),MZM_HEIGHT_TEXT_TOOLBAR);
    mToolbar.SetID(MZ_IDC_TOOLBAR);
    mToolbar.SetButton(0, true, true, L"Done");
    mToolbar.SetButton(1, false, true, L"Update");
    mToolbar.SetButton(2, true, true, L"Cancel");
    AddUiWin(&mToolbar);

	return TRUE;
}

void CLoginDlg::OnMzCommand(WPARAM wParam, LPARAM lParam)
{
	UINT_PTR id = LOWORD(wParam);

	if(id==MZ_IDC_TOOLBAR) {
		switch(lParam) {
		case 0:
			CReaderDatabase::GetSingleton().SetValue(_T("username"), mUserName.GetText().C_Str());
			CReaderDatabase::GetSingleton().SetValue(_T("password"), mPassword.GetText().C_Str());
			g_SubscriptionDlg.Active(MZ_ANIMTYPE_SCROLL_LEFT_TO_RIGHT_1);
			break;
		case 1:
			break;
		case 2:
			g_SubscriptionDlg.Active(MZ_ANIMTYPE_SCROLL_LEFT_TO_RIGHT_1);
			break;
		}
	}
}

void CLoginDlg::Active(UINT nAnimType)
{
	TCHAR szUsername[100];
	TCHAR szPassword[100];

	if(!CReaderDatabase::GetSingleton().GetValue(_T("username"), szUsername, sizeof(szUsername))) {
		_tcscpy(szUsername, _T(""));
	}
	if(!CReaderDatabase::GetSingleton().GetValue(_T("password"), szPassword, sizeof(szPassword))) {
		_tcscpy(szPassword, _T(""));
	}

	mUserName.SetText(szUsername);
	mPassword.SetText(szPassword);

	if(MzGetParam(MZGP_APP_START_ANIMATION)==TRUE) {
		AnimateWindow(nAnimType, true);
    }
	Show();
}
